﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.DoctorCommand;
using D.Medicalcare.Read.API.Dtos;
using MediatR;

namespace D.Medicalcare.Read.API.Application.Handler.DoctorHander
{
    public class GetDoctorHarder : IRequestHandler<GetDoctorCommand, APIPaging<DoctorDto>>
    {

        private readonly IBaseRepository<Doctor> repository;
        private readonly IBaseRepository<TitleManagement> Titlerepository;
        private readonly IBaseRepository<HospitalNavigation> Hosprepository;
        private readonly IBaseRepository<Department> Deparepository;
        
        public GetDoctorHarder(IBaseRepository<Doctor> repository, IBaseRepository<TitleManagement> titlerepository, IBaseRepository<HospitalNavigation> hosprepository, IBaseRepository<Department> deparepository)
        {
            this.repository = repository;
            Titlerepository = titlerepository;
            Hosprepository = hosprepository;
            Deparepository = deparepository;
        }

        public async Task<APIPaging<DoctorDto>> Handle(GetDoctorCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<DoctorDto> result = new APIPaging<DoctorDto>();
                result.Code = APIEnum.Mvc;
                result.Msg = "查询成功";
                var list = from a in repository.GetAll()
                           join b in Deparepository.GetAll() on a.DeparId equals b.DeparId
                           join c in Hosprepository.GetAll() on a.CampusId equals c.HospitalNavigationId
                           join d in Titlerepository.GetAll() on a.TitlemanagementId equals d.TitleManagementId
                           where a.IsDeleted == false
                           select new DoctorDto
                           {



                               DoctorId = a.DoctorId,
                               DoctorName = a.DoctorName,
                               DoctorImage = a.DoctorImage,
                               CampusId = c.HospitalNavigationId,
                               CampusName = c.CampusName,
                               DeparId = b.DeparId,
                               DeparName = b.DeparName,
                               TitlemanagementId = d.TitleManagementId,
                               JobName = d.JobName,
                               Doctorlabel = a.Doctorlabel,
                               Registrationfee = a.Registrationfee,
                               Consultationfee = a.Consultationfee,
                               Doctorintroduc = a.Doctorintroduc,
                               Doctorstate = a.Doctorstate,


                           };

                // 应用筛选条件
                if (!string.IsNullOrEmpty(request.DoctorName))
                {
                    list = list.Where(a => a.DoctorName.Contains(request.DoctorName));
                }
              
                if (request.DeparId != null)
                {
                    list = list.Where(b => b.DeparId == request.DeparId);
                }
                result.Count = list.Count();
                result.PageCount = (int)Math.Ceiling(result.Count * 1.0 / request.PageSize);
                result.Data = list.OrderByDescending(a => a.DoctorId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
